package cn.iocoder.yudao.module.polling.controller.admin.pollingtype;

import cn.iocoder.yudao.module.polling.controller.admin.pollingtype.vo.*;
import cn.iocoder.yudao.module.polling.convert.ids.OrderUtil;
import cn.iocoder.yudao.module.polling.convert.pollingtype.PollingTypeConvert;
import cn.iocoder.yudao.module.polling.dal.dataobject.pollingtype.PollingTypeDO;
import cn.iocoder.yudao.module.polling.service.pollingtype.PollingTypeService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Api(tags = "管理后台 - 巡检类型")
@RestController
@RequestMapping("/abu/polling-type")
@Validated
public class PollingTypeController {

    @Resource
    private PollingTypeService pollingTypeService;

    @PostMapping("/create")
    @ApiOperation("创建巡检类型")
    @PreAuthorize("@ss.hasPermission('abu:polling-type:create')")
    public CommonResult<Integer> createPollingType(@Valid @RequestBody PollingTypeCreateReqVO createReqVO) {
        createReqVO.setPtCode(OrderUtil.getId("PTYPE"));
        return success(pollingTypeService.createPollingType(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新巡检类型")
    @PreAuthorize("@ss.hasPermission('abu:polling-type:update')")
    public CommonResult<Boolean> updatePollingType(@Valid @RequestBody PollingTypeUpdateReqVO updateReqVO) {
        pollingTypeService.updatePollingType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除巡检类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-type:delete')")
    public CommonResult<Boolean> deletePollingType(@RequestParam("id") Integer id) {
        pollingTypeService.deletePollingType(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得巡检类型")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-type:query')")
    public CommonResult<PollingTypeRespVO> getPollingType(@RequestParam("id") Integer id) {
        PollingTypeDO pollingType = pollingTypeService.getPollingType(id);
        return success(PollingTypeConvert.INSTANCE.convert(pollingType));
    }

    @GetMapping("/list")
    @ApiOperation("获得巡检类型列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('abu:polling-type:query')")
    public CommonResult<List<PollingTypeRespVO>> getPollingTypeList(@RequestParam("ids") Collection<Integer> ids) {
        List<PollingTypeDO> list = pollingTypeService.getPollingTypeList(ids);
        return success(PollingTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得巡检类型分页")
    @PreAuthorize("@ss.hasPermission('abu:polling-type:query')")
    public CommonResult<PageResult<PollingTypeRespVO>> getPollingTypePage(@Valid PollingTypePageReqVO pageVO) {
        PageResult<PollingTypeDO> pageResult = pollingTypeService.getPollingTypePage(pageVO);
        return success(PollingTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出巡检类型 Excel")
    @PreAuthorize("@ss.hasPermission('abu:polling-type:export')")
    @OperateLog(type = EXPORT)
    public void exportPollingTypeExcel(@Valid PollingTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PollingTypeDO> list = pollingTypeService.getPollingTypeList(exportReqVO);
        // 导出 Excel
        List<PollingTypeExcelVO> datas = PollingTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "巡检类型.xls", "数据", PollingTypeExcelVO.class, datas);
    }

}
